﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Steam.TF2
{
    /// <summary>
    /// Provides access to Team Fortress 2 specific data.
    /// </summary>
    public class TF2WebClient
    {
        #region private readonly
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private readonly string getSchemaUrl = "http://api.steampowered.com/IEconItems_{0}/GetSchema/v0001/?key={1}&format=xml";
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private readonly string getBackpackUrl = "http://api.steampowered.com/IEconItems_{0}/GetPlayerItems/v0001/?key={1}&format=xml&SteamID={2}";
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private readonly string getTF2PlayerStatsUrl = "http://steamcommunity.com/id/{0}/stats/TF2?xml=1"; 
        #endregion

        #region TF2WebClient
        public TF2WebClient(string apiKey)
        {
            this.ApiKey = apiKey;
        }
        #endregion

        #region ApiKey
        /// <summary>
        /// Gets the API key for this client.
        /// </summary>
        /// <value>The API key.</value>
        public string ApiKey { get; private set; }
        #endregion

        #region GetSchema
        /// <summary>
        /// Gets the schema for the specified Steam game.
        /// </summary>
        /// <param name="gameId">The game id.</param>
        /// <returns></returns>
        public TF2Schema GetSchema()
        {
            System.Net.WebClient client = new System.Net.WebClient();

            string xml = client.DownloadString(string.Format(getSchemaUrl, 440, this.ApiKey));

            return TF2Schema.Deserialize(xml);
        }
        #endregion

        #region GetPlayerBackpack
        /// <summary>
        /// Gets the player backpack contents.
        /// </summary>
        /// <param name="steamId">The steam id as a long integer.</param>
        /// <returns></returns>
        public TF2Backpack GetPlayerBackpack(long steamId64)
        {
            System.Net.WebClient client = new System.Net.WebClient();
            string xml = client.DownloadString(string.Format(getBackpackUrl, 440, this.ApiKey, steamId64));

            return TF2Backpack.Deserialize(xml);
        }
        #endregion

        #region GetTF2PlayerStats
        public TF2PlayerStats GetTF2PlayerStats(string profileName)
        {
            System.Net.WebClient client = new System.Net.WebClient();
            string xml = client.DownloadString(string.Format(getTF2PlayerStatsUrl, profileName));

            return TF2PlayerStats.Deserialize(xml);
        } 
        #endregion
    }    
}
